package com.hbnu.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author kittychen
 * @since 2025-10-13 下午 3:26
 */
public class ArrayListDemo {

    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>();

        arrayList.add("a");
        arrayList.add("r");
        arrayList.add("y");
        arrayList.add("t");
        arrayList.add("p");
        arrayList.add("a");
        System.out.println(arrayList);

        System.out.println(arrayList.size());
        System.out.println(arrayList.isEmpty());
        System.out.println(arrayList.contains("o"));
        System.out.println(arrayList.indexOf("t"));
        System.out.println(arrayList.get(2));
        arrayList.set(0,"china");
        arrayList.add(1, "beautiful");
        System.out.println(arrayList);
        arrayList.remove(2);
        System.out.println(arrayList);

        // 遍历集合元素
        System.out.println("普通for循环遍历集合元素");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }

        System.out.println("增强for循环遍历集合元素");
        for (String element : arrayList) {
            System.out.println(element);
        }

        System.out.println("迭代器遍历集合元素");
        Iterator<String> iterator = arrayList.iterator();  // 获取迭代器
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
